import * as fs from "node:fs"
import * as path from "node:path"
import { Pixel } from "../hp3-img.js";
import { savePng } from "../hp3-png.js";
import { loadImage } from "../hp3-imp.js";

const inputDir = "E:\\aigc\\output\\序列帧\\ok-large";
const outputDir = "E:\\aigc\\output\\序列帧\\ok-auto";

const pattern = /m.d-ok-l\.png/;
const dys = 0;
const dye = 560;
const dxs = 104;
const dxe = 1224;

const dw = dxe-dxs;
const dh = dye-dys;

if(!fs.existsSync(outputDir)){
    fs.mkdirSync(outputDir);
}

const files = fs.readdirSync(inputDir);
for(const file of files){
    if(file.match(pattern)){
        console.error("开始处理："+file);
        const inputFile = path.join(inputDir, file);
        const outputFile = path.join(outputDir, file);
        const src = await loadImage(inputFile);
        const pixels = new Array<Pixel>(dw*dh);
        for(let y=0; y<dh; y++){
            for(let x=0; x<dw; x++){
                pixels[y*dw+x] = src.pixels[(y+dys)*src.width+x+dxs];
            }
        }
        savePng(dw,dh,pixels,outputFile);
        console.error("处理完成："+file);
    }
}
